#include "threadPool.h"
int makeWorker(threadPool_t *threadPool){
    for(int i = 0; i < threadPool->workerNum; ++i){
        pthread_create(&threadPool->tidArr[i],NULL,threadFunc,threadPool);
    }
    return 0;
}


void *threadFunc(void *arg){
    threadPool_t *threadPool = (threadPool_t *)arg;
    char cmd[10]={0};
    char client_arg[100]={0};
    char buf[100]={0};
    char name[30]={0};
    char password[100]={0};
    int user_id=-1;
    int file_id=-1;
    int password_check;//登录返回值

    pthread_mutex_lock(&threadPool->taskQueue.mutex);
    MYSQL * mysql = mysql_init(NULL);
    MYSQL * ret_connect = mysql_real_connect(mysql,"localhost","root","743957","server_db",0,NULL,0);
    if(ret_connect==NULL){
        fprintf(stderr,"mysql connect error:%s\n",mysql_error(mysql));
    }
    pthread_mutex_unlock(&threadPool->taskQueue.mutex);
    //每个线程连接一个数据库

    while(1){
        pthread_mutex_lock(&threadPool->taskQueue.mutex);
        while(threadPool->taskQueue.queueSize == 0&&threadPool->exitflag==0){
            pthread_cond_wait(&threadPool->taskQueue.cond, &threadPool->taskQueue.mutex);
        }
        if(threadPool->exitflag != 0){
            printf("child is going to exit!\n");
            pthread_mutex_unlock(&threadPool->taskQueue.mutex);
            pthread_exit(NULL);
        }
        printf("child get a task!\n");
        int netfd = threadPool->taskQueue.pFront->netfd;
        deQueue(&threadPool->taskQueue);
        pthread_mutex_unlock(&threadPool->taskQueue.mutex);


        while(1)
        {
            memset(name,0,sizeof(name));
            memset(password,0,sizeof(password));
            recv_msg(netfd,name);
            recv_msg(netfd,password);
            //接收用户名和密码
            int pass_ret=passwd_check(name,password,mysql,&user_id);
            //printf("%d\n",password_check);
            if(pass_ret==-1){
                send_msg(netfd,"密码错误!");
                while(1){
                    bzero(password,sizeof(password));
                    recv_msg(netfd,password);
                    //等待新密码
                    pass_ret=passwd_check(name,password,mysql,&user_id);
                    //判断是否正确
                    if(pass_ret==-1){
                        send_msg(netfd,"密码错误");
                        continue;
                    }
                    break;
                }
            }else if(pass_ret==1){
                send_msg(netfd,"注册成功!");
                char temp[100]={0};
                strcat(temp,name);
                strcat(temp,"/");

                int mkdir_ret=mkdir_server(name,temp,name,&file_id,mysql); 
                if(mkdir_ret==-1)
                {
                    sprintf(buf,"make the wrong directory");
                }
                else
                {
                    ls(mysql,client_arg,&file_id,buf);
                }
                send_msg(netfd,buf);
            }else {
                send_msg(netfd,"登录成功!");
                change_id(name,&file_id,-1,mysql); 
                break;
            }
        }
        while(1)
        {
            char server_exit[]="yes";
            send(netfd,server_exit,sizeof(server_exit),0);

            memset(cmd,0,sizeof(cmd));
            memset(client_arg,0,sizeof(client_arg));
            bzero(buf,sizeof(buf));

            recv_msg(netfd,cmd);
            recv_msg(netfd,client_arg);

            NETDISK_LOG_INFO(name,cmd,client_arg);

            if(cmd[0]=='\0')
            {
                break;
            }
            if(strcmp(cmd,"cd")==0)
            {
                int cd_ret=cd(mysql,client_arg,&file_id,buf);
                printf("%s\n",buf);
                if(cd_ret==-1)
                {
                    sprintf(buf,"cd the wrong directory");
                }
                send_msg(netfd,buf);
            }
            else if(strcmp(cmd,"mkdir")==0)
            {
                char path_pwd[100]={0};
                pwd(&file_id,path_pwd,mysql);
                int mkdir_ret=mkdir_server(client_arg,path_pwd,name,&file_id,mysql);
                if(mkdir_ret==-1)
                {
                    sprintf(buf,"make the wrong directory");
                }
                else
                {
                    ls(mysql,"\0",&file_id,buf);
                }
                send_msg(netfd,buf);
            }
            else if(strcmp(cmd,"ls")==0)
            {
                int ls_ret= ls(mysql,client_arg,&file_id,buf);
                if(ls_ret==-1)
                {
                    sprintf(buf,"ls the wrong directory");
                }
                send_msg(netfd,buf);
            }
            else if(strcmp(cmd,"puts")==0)
            {
                char path_file[100]={"file/"};
                char path_user[100]={0};
                char command_puts[1024]={0};
                char md5[33]={0};
                recv_msg(netfd,md5);

                pwd(&file_id,path_user,mysql);
                strcat(path_user,client_arg);

                sprintf(command_puts,"insert into file_tbl (filename,user_id, pre_id,type,path,is_delete,md5) values ('%s','%s',%d,'file','%s','1','%s');",client_arg,name,file_id,client_arg,md5);
                mysql_query(mysql,command_puts);

                strcat(path_file,md5);
                getsfile(netfd,path_file); 
            }
            else if(strcmp(cmd,"gets")==0)
            {
                recv_msg(netfd,client_arg);
                //收到文件名
                char path_gets[100]={0};
                char path_filename[100]={0};
                char command_getmd5[100]={"select md5 from server_db where user = "};
                pwd(&file_id,path_gets,mysql);
                //得到当前用户目录
                strcat(path_gets,client_arg);
                //合并当前目录和文件名
                strcat(command_getmd5,name);
                strcat(command_getmd5," and path = ");
                strcat(command_getmd5,path_gets);

                int qret=mysql_query(mysql,command_getmd5);
                MYSQL_RES *result=mysql_store_result(mysql);
                if(qret!=0){
                    fprintf(stderr,"SQL statement error : %s\n",mysql_error(mysql));
                    send(netfd,"-1",1,0);
                    continue;
                }else{
                    MYSQL_ROW row = mysql_fetch_row(result);
                    strcat(path_filename,"file/");
                    strcat(path_filename,*row);
                }
                transfile(netfd,path_filename);
            }
            else if(strcmp(cmd,"pwd")==0)
            {
                char path_pwd[100]={0};
                pwd(&file_id,path_pwd,mysql);
                send_msg(netfd,path_pwd);
            }
            else if(strcmp(cmd,"remove")==0)
            {
                char path_pwd[100]={0};
                pwd(&file_id,path_pwd,mysql);
                int ret_remove = remove_server(client_arg,path_pwd,name,mysql);
                if(ret_remove==-1){
                    send_msg(netfd,"删除失败!\n");
                }else{
                    send_msg(netfd,"删除成功!\n");
                }
            }
            else if(strcmp(cmd,"rmdir")==0)
            {
                char path_pwd[100]={0};
                pwd(&file_id,path_pwd,mysql);
                int ret_rmdir = rmdir_server(client_arg,path_pwd,name,&file_id,mysql);
                if(ret_rmdir==-1)
                {
                    sprintf(buf,"删除失败\n");
                }
                else
                {
                    sprintf(buf,"删除成功");
                }
                send_msg(netfd,buf);
            }else if(strcmp(cmd,"tree")==0){

                int tree_ret=tree(mysql,&file_id,buf,0);
                if(tree_ret==-1){
                    sprintf(buf,"打开树失败!");
                }
                send_msg(netfd,buf);
            }
            printf("child finish a task!\n");
        }
        printf("用户已退出连接\n");
        close(netfd);
    }
}
